package cn.yotta.user.core.server.service.common.impl;

import cn.yotta.user.api.params.captcha.CaptchaCheckRequest;
import cn.yotta.user.api.vo.CaptchaVo;
import cn.yotta.user.core.server.dto.captcha.CaptchaCheckDto;
import cn.yotta.user.core.server.dto.captcha.CaptchaDto;
import cn.yotta.user.core.server.manager.captcha.CaptchaManager;
import cn.yotta.user.core.server.service.common.CaptchaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author wyaoyao
 * @date 2022/11/9 10:12
 */
@Service
@Slf4j
public class CaptchaServiceImpl implements CaptchaService {

    private final CaptchaManager captchaManager;

    public CaptchaServiceImpl(CaptchaManager captchaManager) {
        this.captchaManager = captchaManager;
    }


    @Override
    public CaptchaVo genCaptcha() {
        final CaptchaDto captchaDto = captchaManager.genCaptcha();
        return CaptchaVo.builder()
                .token(captchaDto.getToken())
                .imageUrl(captchaDto.getImageUrl())
                .text(captchaDto.getText())
                .build();
    }

    @Override
    public Boolean checkCaptcha(CaptchaCheckRequest request) {
         captchaManager.checkCaptcha(CaptchaCheckDto.builder()
                .token(request.getToken())
                .value(request.getValue())
                .build());
        return Boolean.TRUE;
    }

}
